<template>
	<view style="width: 100%;">
		<view class="input_view">
			<view class="iv" style="margin-right:20px;">+86</view>
			<input class="input" placeholder="手机号" v-model="mobile" :disabled="isedit"/>
		</view>
		<view class="input_view">
			<input class="input"  v-model="code"/> 
			<view style="width: 220rpx;" class="iv"
				@click="getCode"
				:class="codeTime>0 ?'bg-d':'bg'">
					{{codeTime>0 ? codeTime: '获取验证码'}}
			</view>
		</view>
		
		<view style="padding: 0 10px; width:calc(100% - 20px);" >
			<button style="border: none;" @click="submit" :disabled="!(code!='' && mobile!='')" :class="(code!='' && mobile!='') ? 'bg-d':'bg2'">确定</button>
		</view>
	</view>
</template>

<script>
export default{
	data() {
		return {
			ij:{},
			mobile:'',
			codeTime:0,
			isedit:false,
			code:'',
			lr:{},
			password:'',
			loginId:'',
			openpageBtnInfo:{},
			regResult:''
		}
	},
	onLoad:async function(o){
		console.log(o)
		var ij = JSON.parse(o.ij)
		var mobile = o.mobile
		this.ij = ij
		this.mobile = mobile
		this.lr = o.lr
		this.loginId = o.loginId
		this.password = o.password
		this.regResult=o.regResult
		this.openpageBtnInfo = o.openpageBtnInfo
		if(mobile!='') this.isedit = true
		
	},
	methods:{
		
		async getCode(){
			var msg = this.checkmobile()
			if(msg!=''){
				uni.showModal({
					title:'提示',
					content:msg,
					showCancel:false
				})
				return
			}
			if (this.codeTime>0) return;
			
			var ij = this.ij
			ij['mobile'] = this.mobile
			var url = getApp().globalData.app_address + '?method=saas_Login_2SMSGetPin&parse=json'
			this.$post2(url, JSON.stringify(ij));
			
			
			
			this.codeTime = 60
			let timer = setInterval(()=>{
				if (this.codeTime >= 1){
					this.codeTime--
				} else{
					clearInterval(timer)
				}
			},1000)
		},
		
		checkmobile(){
			var msg = ''
			if(this.mobile==''){
				msg = '手机号不能为空'
				return msg
			}
			msg = this.$isMobileNumber(this.mobile)
			return msg
		},
		
		async submit(){
			var msg = this.checkmobile()
			if(msg!=''){
				uni.showModal({
					title:'提示',
					content:msg,
					showCancel:false
				})
				return
			}
			var ij = this.ij
			ij['mobile'] = this.mobile
			ij['smsPin'] = this.code
			ij['hostMacAll'] = ''
			ij['hostVolumeID'] = ''
			ij['hostDiskID'] = ''

			var url = getApp().globalData.app_address + '?method=saas_Login_3SMSCheckPin&parse=json'
			const { data: res } = await this.$post2(url, JSON.stringify(ij));
			console.log(res)
			if(res.rtn == 1){
				ij['regResult'] = this.regResult
				var url = getApp().globalData.app_address + '?method=saas_Login_4UpdateUserDevice&parse=json'
				const { data: res4 } = await this.$post2(url, JSON.stringify(ij));
				console.log(res4)
				if(res4.rtn == 1){
					this.loginres(this.lr, this.password,this.loginId)
				}else{
					uni.showModal({
						title: '提示',
						content: res4.message,
						showCancel:false
					});
				}
				
			}else{
				uni.showModal({
					title: '提示',
					content: res.message,
					showCancel:false
				});
			}
		},
		
		loginres(lrstr,password,loginId){
			var that = this
			var lr = JSON.parse(lrstr)
			uni.setStorageSync('sessionId',lr.sessionId)
			uni.setStorageSync('userId',lr.userId)
			uni.setStorageSync('userName',lr.userName)
			uni.setStorageSync('appVersion',lr.appVersion)
			uni.setStorageSync('accountid', loginId);
			uni.setStorageSync('wx_appid', lr.wx_appid);
			uni.setStorageSync('wx_serviceid', lr.wx_serviceid);
			uni.setStorageSync('partnerCode', lr.partnerCode!=undefined?lr.partnerCode:'');
			uni.setStorageSync('partnerName', lr.partnerName!=undefined?lr.partnerName:'');
			var isremeberpwd = uni.getStorageSync('isremeberpwd');
			if (isremeberpwd == 'true') {
				uni.setStorageSync('password', password);
			}
			var openpageBtnInfo = JSON.parse(this.openpageBtnInfo)
			var clicked = openpageBtnInfo.clicked
			var pageName2 = clicked.pageName
			var templetName2 = clicked.templetName
			var expArgs = clicked.expArgs
			if(templetName2 == 'appsj_index' || templetName2 == 'appsj_list' || templetName2 == 'appsj_index_pda'){
				uni.setStorageSync('pageName',pageName2)
				uni.setStorageSync('templetName',templetName2)
				uni.setStorageSync('modid', openpageBtnInfo.powerModId)
				uni.setStorageSync('menuid', openpageBtnInfo.powerModCode)
				uni.setStorageSync('expArgs',expArgs)
				uni.setStorageSync('indexTempletName', templetName2)
				if(templetName2 == 'appsj_index' || templetName2=='appsj_index_pda'){
					uni.switchTab({url:'../index/index'})
				}else if(templetName2 == 'appsj_list'){
					uni.reLaunch({
						url:'../infolist/infolist?templatename='+templetName2+'&pagename='+pageName2+'&menuid='+openpageBtnInfo.powerModCode+'&menuname=首页&modid='+openpageBtnInfo.powerModId
					})
				}
			}
		}
	}
}
</script>

<style>
	.input_view{ margin: 10px;padding: 5px 10px;border: 1px solid #D9D9D9;width:calc(100vw - 40px);border-radius: 5px;display:flex;flex-direction: row;}
	.iv{width: 30rpx;display: flex;align-items: center;flex-direction: row;justify-content: center;}
	.input{outline-color: invert;flex:1; outline-style: none;outline-width: 0px;border: none;border-style: none;text-shadow: none; -webkit-appearance: none;-webkit-user-select: text;outline-color: transparent;box-shadow: none;outline: none;border-radius: 0;}
	.bg-d{background-color:#fd6137;color:#fff;}
	.bg{background-color: cornsilk;color: #666;}
	.bg2{background-color: gray;color: #ccc;}
</style>